<?php require_once('common.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends Common {
	function __construct() {
		parent::__construct("admin");
		$this->load->model(array('user'));
	}
	
	public function index(){
		$data['petugas'] = $this->user->get()->result_array();
		$data['role'] = unserialize(ROLE_PETUGAS);
		$data['pesan'] = $this->session->flashdata('pesan');
		$this->load->view('admin/list_petugas', $data);
	}
	
	function add_petugas(){
		$data['mode'] = 'ADD';
		$data['role'] = unserialize(ROLE_PETUGAS);
		$data['pendidikan'] = unserialize(PENDIDIKAN);
		$this->load->view('admin/form_petugas',$data);
	}

	function edit_petugas($id){
		$data['mode'] = 'EDIT';
		$data['detail'] = $this->user->get(array('id_petugas' => $id))->row_array();
		$data['role'] = unserialize(ROLE_PETUGAS);
		$data['pendidikan'] = unserialize(PENDIDIKAN);
		$this->load->view('admin/form_petugas',$data);	
	}
	
	function save_petugas(){
		$this->layout = FALSE;
		$mode = $this->input->post('mode');
		$data = array('nama' 		=> $this->input->post('nama'),
					  'no_telp' => $this->input->post('no_telp'),
					  'tgl_lahir'	=> $this->input->post('tgl_lahir'),
					  'alamat'	=> $this->input->post('alamat'),
					  'pendidikan_terakhir'	=> $this->input->post('pendidikan_terakhir'),
					  'username' => $this->input->post('username'));
		
		if($mode == 'ADD'){
			$data['password'] = $this->input->post('password');
			$success = $this->user->add($data);
		}else{
			$id = $this->input->post('id_petugas');
			if($this->input->post('password') != ""){
				$data['password'] = $this->input->post('password');
			}
			$success = $this->user->edit($id, $data);
		}
		
		if($success){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Data Petugas berhasil disimpan."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Data Petugas gagal disimpan."));
		}
		redirect(base_url()."index.php/admin");
	}
	
	function delete_petugas($id){
		if($this->user->delete($id)){
			$this->session->set_flashdata('pesan', array('success' =>true, 'pesan' => "Petugas berhasil dihapus."));
		}else{
			$this->session->set_flashdata('pesan', array('success' =>false, 'pesan' => "Petugas gagal dihapus."));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}